Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
ansi-sequence-parser
Advanced tools
ansi-sequence-parser
Parse ANSI escape sequences into a readable format for things like generating pretty HTML.
Install with your favourite package manager:
pnpm install ansi-sequence-parser
yarn add ansi-sequence-parser
npm install ansi-sequence-parser
Token format:
interface ParseToken {
// The text content of the token
value: string;
// The foreground color
foreground: Color | null;
// The background color
background: Color | null;
// A Set of the applied decorations
decorations: Set<DecorationType>;
}
Parse full input at once:
import { parseAnsiSequences } from 'ansi-sequence-parser';
const tokens = parseAnsiSequences(input);
If you want to parse your input in multiple chunks, make sure to create a parser so that you can maintain state between chunks:
import { createAnsiSequenceParser } from 'ansi-sequence-parser';
const parser = createAnsiSequenceParser();
const tokensByLine = input.split(/\r?\n/).map((line) => parser.parse(line));
Colors format:
// A named ANSI color, e.g. `magenta` or `brightBlue`
export interface NamedColor {
type: 'named';
name: ColorName;
}
// A color-table lookup
export interface TableColor {
type: 'table';
index: number;
}
// An RGB color
export interface RgbColor {
type: 'rgb';
rgb: [number, number, number];
}
export type Color = NamedColor | TableColor | RgbColor;
In order to interpret all of the above color types as a hex code, you can create a color palette:
import { parseAnsiSequences, createColorPalette } from 'ansi-sequence-parser';
const tokens = parseAnsiSequences(input);
const colorPalette = createColorPalette();
for (const token of tokens) {
if (token.foreground) {
const foregroundValue = colorPalette.value(token.foreground);
}
if (token.background) {
const backgroundValue = colorPalette.value(token.background);
}
}
You can also specify a custom named colors map:
import { parseAnsiSequences, createColorPalette } from 'ansi-sequence-parser';
const tokens = parseAnsiSequences(input);
const colorPalette = createColorPalette({
black: '#000000',
red: '#bb0000',
green: '#00bb00',
yellow: '#bbbb00',
blue: '#0000bb',
magenta: '#ff00ff',
cyan: '#00bbbb',
white: '#eeeeee',
brightBlack: '#555555',
brightRed: '#ff5555',
brightGreen: '#00ff00',
brightYellow: '#ffff55',
brightBlue: '#5555ff',
brightMagenta: '#ff55ff',
brightCyan: '#55ffff',
brightWhite: '#ffffff',
});
If you want to modify the default named colors map, you can import the defaultNamedColorsMap
:
import {
parseAnsiSequences,
createColorPalette,
defaultNamedColorsMap,
} from 'ansi-sequence-parser';
const tokens = parseAnsiSequences(input);
const colorPalette = createColorPalette({
...defaultNamedColorsMap,
blue: '#0000cc',
});
1.1.1
FAQs
A parser for ANSI escape sequences
We found that ansi-sequence-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.